package filter;

import pojo.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Administrator on 2017-11-20.
 */
public class Filter implements javax.servlet.Filter {
    private String encode="UTF-8";
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;



//        request.setCharacterEncoding(encode);
//        response.setCharacterEncoding(encode);
//        response.setContentType("text/html;charset/"+encode);
        HttpSession session=request.getSession();
        String path = request.getRequestURI();
        User user = (User) session.getAttribute("userInfo");

//         登陆页面无需过滤
        if(path.indexOf("/index.jsp") > -1||path.indexOf("login")>-1) {
            chain.doFilter(request, response);
            return;
        }
        // 判断如果没有取到员工信息,就跳转到登陆页面
        if (user == null ) {
            // 跳转到登陆页面
            response.sendRedirect("/jsp/index.jsp");
        } else {
            // 已经登陆,继续此次请求
            chain.doFilter(request, response);
        }

    }

    @Override
    public void destroy() {

    }
}
